package zhoug.zui.group;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.view.View;

import zhoug.zui.R;

import androidx.annotation.Nullable;

/**
 * 弧度
 *
 * @Author 35574
 * @Date 2020/11/24
 * @Description
 */
public class ZUIArcLinearLayout extends ZUILineaLayout {
    private float sizeRatio = 0.2f;//弧度占容器高度的百分比
    private float arcSize;//弧度的高度优先级高于sizeRatio
    /**
     * 弧度在容器的哪个方向
     */
    private int arcGravity;
    public static final int GRAVITY_TOP=1;//上方
    public static final int GRAVITY_BOTTOM=2;//下方

    public ZUIArcLinearLayout(Context context) {
        this(context, null);
    }

    public ZUIArcLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZUIArcLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUIArcLinearLayout, defStyleAttr, 0);
        sizeRatio=a.getFloat(R.styleable.ZUIArcLinearLayout_zui_sizeRatio,0.2f);
        arcSize=a.getDimension(R.styleable.ZUIArcFrameLayout_zui_arcSize,0);

        arcGravity=a.getInt(R.styleable.ZUIArcLinearLayout_zui_arcGravity,GRAVITY_BOTTOM);
        if(sizeRatio<0){
            sizeRatio=0;
        }else if(sizeRatio>1){
            sizeRatio=1;
        }

        a.recycle();
        //硬件加速,否则会有黑色背景
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        arcPaint.setAntiAlias(true);
    }

    private Paint arcPaint = new Paint();
    private Bitmap acrBitmap;//圆弧路径图片
    private Xfermode DST_IN = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (sizeRatio > 0) {
            acrBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
            Canvas canvas = new Canvas(acrBitmap);
            Path path = new Path();
            if(arcGravity==GRAVITY_BOTTOM){
                float acrH = h * (1 - sizeRatio);
                path.moveTo(0, 0);
                path.lineTo(w, 0);
                path.lineTo(w, acrH);
                path.quadTo(w >> 1, h, 0, acrH);
                path.lineTo(0, 0);
            }else if(arcGravity==GRAVITY_TOP){
                float acrH =sizeRatio*h;
                path.moveTo(0,acrH);
                path.quadTo(w>>1,0,w,acrH);
                path.lineTo(w,h);
                path.lineTo(0,h);
                path.lineTo(0,acrH);
            }

            arcPaint.setXfermode(null);
            arcPaint.setColor(Color.parseColor("#ffffff"));
            canvas.drawPath(path, arcPaint);
        }

    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (acrBitmap != null) {
            arcPaint.setXfermode(DST_IN);
            canvas.drawBitmap(acrBitmap, 0, 0, arcPaint);
        }
    }

}

